﻿<?php

session_start();
include("../public_files/conn.php");
date_default_timezone_set("Asia/ShangHai");
$time=date('Y-m-d H:i:s');
mysql_query("set names UTF8")or die(mysql_error());
/* header("Content-Type: text/xml;charset=utf-8");
header("Cache-Control: no-cache");
 */




if(isset($_POST['x'])){
$status=$_POST['status'];
	$listname_id=$_POST['listname_id'];//榜单名ID 
	 $listname=$_POST['listname'];
	$introduction=$_POST['introduction'];
	$X=$_POST['x'];//X列
	$Y=$_POST['y']; //Y条
	$sort_id=$_POST['sort_id'];
	
	
	if($listname_id==1){
		$list_sort_id=$sort_id;
		if(isset($_SESSION['status']) && $_SESSION['status']==1){
			$user_id=$_SESSION['userid'];
		}else{
		$user_id=0; 	
		}
		$user_id;
		//插入榜单名
			$sql="insert into  user_list_name (list_name,user_id,list_sort_id,introduction,status,create_time) values ('$listname','$user_id','$list_sort_id','$introduction','$status','$time')";
			$result=mysql_query($sql)or die(mysql_error()); 
			$query="SELECT LAST_INSERT_ID()";
			$result=mysql_query($query);
			$rows=mysql_fetch_row($result);
			$list_id=$rows[0];//榜单id
			
			//添加图片mode
			$sql1="insert into user_list_mode (mode_name,name_id) values ('image','$list_id')";
			mysql_query($sql1)or die(mysql_error());
			$query="SELECT LAST_INSERT_ID()";
			$result=mysql_query($query);
			$rows=mysql_fetch_row($result);
			$mode_id[]=$rows[0];//图片modeID
		//插入mode
		for($i=1;$i<=$X;$i++){
			  $mode="mode".$i;
			  $mode_name=$_POST[$mode];

			$sql1="insert into user_list_mode (mode_name,name_id) values ('$mode_name','$list_id')";
			mysql_query($sql1)or die(mysql_error());
			$query="SELECT LAST_INSERT_ID()";
			$result=mysql_query($query);
			$rows=mysql_fetch_row($result);
			
			$mode_id[]=$rows[0];//记录mode起始ID
		}
//判断是否有more		
		if(isset($_POST['more'])){				
			  $mode_name="more";
			$sql1="insert into user_list_mode (mode_name,name_id) values ('$mode_name','$list_id')";
			mysql_query($sql1)or die(mysql_error());
			$query="SELECT LAST_INSERT_ID()";
			$result=mysql_query($query);
			$rows=mysql_fetch_row($result);
			$mode_id[]=$rows[0];//记录mode起始ID
		}
		
		//添加顶和踩
		$sql1="insert into user_list_mode (mode_name,name_id) values ('ding','$list_id')";
			mysql_query($sql1)or die(mysql_error());
			$query="SELECT LAST_INSERT_ID()";
			$result=mysql_query($query);
			$rows=mysql_fetch_row($result);
			$mode_id[]=$rows[0];//ding--modeID
			
		$sql1="insert into user_list_mode (mode_name,name_id) values ('cai','$list_id')";
			mysql_query($sql1)or die(mysql_error());
			$query="SELECT LAST_INSERT_ID()";
			$result=mysql_query($query);
			$rows=mysql_fetch_row($result);
			$mode_id[]=$rows[0];//cai--modeID
			


	}else{

		$sql_1="select * from user_list_name where list_name='$listname' order by create_time desc";
		$result_1=mysql_query($sql_1)or die(mysql_error);
		while($row_1=mysql_fetch_array($result_1)){
			$list_id=$row_1['id'];
		}
			$sql1_1="select * from user_list_mode where name_id='$list_id' ";
			$result1_1=mysql_query($sql1_1)or die(mysql_error());
		
			while($row1_1=mysql_fetch_array($result1_1)){
			$mode_id[]=$row1_1['id'];
			
			}



	}
	
			//插入图片路径
			$image_id=$mode_id[0];
			$image_path=$_POST['img_path'];
			$sql3="insert into user_list_content  (list_name_id,mode_id,content,ranking ) values ('$list_id','$image_id','$image_path','$Y')";
			mysql_query($sql3)or die(mysql_error());
		 
	
	for($j=1;$j<=$X;$j++){

 		$input="text".$Y."_".$j;
		$content=$_POST[$input];
		$ID=$j;
		$modeID=$mode_id[$ID];	
		$sql3="insert into user_list_content  (list_name_id,mode_id,content,ranking ) values ('$list_id','$modeID','$content','$Y')";
		mysql_query($sql3)or die(mysql_error()); 
		

	}
	if(isset($_POST['more'])){
		$content=$_POST['more'];
		$ID=($X+1);
		$modeID=$mode_id[$ID];	
		$sql3="insert into user_list_content  (list_name_id,mode_id,content,ranking ) values ('$list_id','$modeID','$content','$Y')";
		mysql_query($sql3)or die(mysql_error()); 
	
	
	
	}
	
	
	
	//插入ding
		$ID=$X+1;
		$modeID=$mode_id[$ID];
		$sql3="insert into user_list_content  (list_name_id,mode_id,content,ranking ) values ('$list_id','$modeID','0','$Y')";
		mysql_query($sql3)or die(mysql_error()); 
	//插入cai
		$ID=$X+2;
		$modeID=$mode_id[$ID];
		$sql3="insert into user_list_content  (list_name_id,mode_id,content ,ranking) values ('$list_id','$modeID','0','$Y')";
		mysql_query($sql3)or die(mysql_error());

		$mode_id=array();	//清空数组
		

echo $sort_id.'|'.$list_id;


}

  




function replace($str){
 $str=str_replace("'","’",$str);
 $str=str_replace(",","",$str);
 $str=str_replace("‘","",$str);
 $str=str_replace("’","",$str);
 return $str;
}








?>